const express = require("express")
const mongoose = require('mongoose')
const bodyParser = require('body-parser')
const passport = require('passport')
const admin = require('./router/api/admin')
const doctor = require('./router/api/doctor')
const app = express();

mongoose.connect('mongodb://localhost/health_server')
    .then(() => console.log('数据库连接成功'))
    .catch(err => console.log(err, '连接失败'))
    require('./config/passportbulid')(passport)
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(passport.initialize())
app.all('/server/*', function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*')
    res.header('Access-Control-Allow-Headers', 'X-Requested-With, mytoken')
    res.header('Access-Control-Allow-Headers', 'X-Requested-With, Authorization')
    res.setHeader('Content-Type', 'application/json;charset=utf-8')
    res.header('Access-Control-Allow-Headers', 'Content-Type,Content-Length, Authorization, Accept,X-Requested-With')
    res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
    res.header('X-Powered-By', ' 3.2.1')
    if (req.method == 'OPTIONS') res.send(200)
    /*让options请求快速返回*/ else next()
  })
app.use('/server/admin',admin)
app.use('/server/doctor',doctor)
const port = process.env.PORT || 6000;
app.listen(port, () => {
    console.log(`服务器已启动 ${port}`)
})